package cn.yyh.servicePet.utils;

import cn.yyh.servicePet.domain.Employee;
import cn.yyh.servicePet.domain.Pet;
import cn.yyh.servicePet.domain.Shop;
import cn.yyh.servicePet.domain.User;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


/**
 * 实体转化工具类ConvertUtils
 *
 * @version: 1.0
 * @return: null
 * @author: 杨永航
 * @date: 2021/7/11
 */
public class ConvertUtils {

    public static JSONObject user(String info){
        return JSONObject.parseObject(info).getJSONObject("data").getJSONObject("user");
    }

    public static JSONObject pet(String info){
        return JSONObject.parseObject(info).getJSONObject("data").getJSONObject("pet");
    }

    public static JSONObject emp(String info){
        return JSONObject.parseObject(info).getJSONObject("data").getJSONObject("emp");
    }

    public static JSONArray shopListAll(String info){
        return JSONObject.parseObject(info).getJSONObject("data").getJSONArray("rows");
    }

    public static User toUser(JSONObject info) {
        User user = new User();
        if ((info != null) && (!info.isEmpty())) {
            if (null != info.get("id")) {
                user.setId(Long.valueOf(String.valueOf(info.get("id"))));

            }
            if (null != info.get("username")) {
                user.setUsername(String.valueOf(info.get("username")));

            }
            if (null != info.get("email")) {
                user.setEmail(String.valueOf(info.get("email")));

            }
            if (null != info.get("phone")) {
                user.setPhone(String.valueOf(info.get("phone")));

            }
            if (null != info.get("salt")) {
                user.setSalt(String.valueOf(info.get("salt")));

            }
            if (null != info.get("password")) {
                user.setPassword(String.valueOf(info.get("password")));
            }
            if (null != info.get("state")) {
                user.setState(Integer.valueOf(String.valueOf(info.get("state"))));
            }
            if (null != info.get("createTime")) {
                try {
                    user.setCreateTime(DateFormat.getDateTimeInstance().parse(String.valueOf(info.get("createTime"))));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if (null != info.get("loginInfoId")) {
                user.setLoginInfoId(Long.valueOf(String.valueOf(info.get("loginInfoId"))));
            }
            if (null != info.get("age")) {
                user.setAge(Integer.valueOf(String.valueOf(info.get("age"))));
            }
            if (null != info.get("headImg")) {
                user.setHeadImg(String.valueOf(info.get("headImg")));

            }
            return user;
        }
        return null;
    }

    public static Pet toPet(JSONObject info) {
        Pet pet = new Pet();
        if ((info != null) && (!info.isEmpty())) {
            if (null != info.get("id")) {
                pet.setId(Long.valueOf(String.valueOf(info.get("id"))));
            }
            if (null != info.get("name")) {
                pet.setName(String.valueOf(info.get("name")));
            }
            if (null != info.get("resources")) {
                pet.setResources(String.valueOf(info.get("resources")));
            }
            if (null != info.get("state")) {
                pet.setSaleprice(new BigDecimal(String.valueOf(info.get("state"))));
            }
            if (null != info.get("costprice")) {
                pet.setCostprice(new BigDecimal(String.valueOf(info.get("costprice"))));
            }
            if (null != info.get("saleprice")) {
                pet.setSaleprice(new BigDecimal(String.valueOf(info.get("saleprice"))));
            }
            if (null != info.get("type_id")) {
                pet.setType_id(Long.valueOf(String.valueOf(info.get("type_id"))));
            }
            if (null != info.get("shop_id")) {
                pet.setShop_id(Long.valueOf(String.valueOf(info.get("shop_id"))));
            }
            if (null != info.get("shop_id")) {
                pet.setShop_id(Long.valueOf(String.valueOf(info.get("shop_id"))));
            }
            if (null != info.get("user_id")) {
                pet.setUser_id(Long.valueOf(String.valueOf(info.get("user_id"))));
            }
            if (null != info.get("search_master_msg_id")) {
                pet.setSearch_master_msg_id(Long.valueOf(String.valueOf(info.get("search_master_msg_id"))));
            }
//            if (null != info.get("offsaletime")) {
//                pet.setOffsaletime(new Date(Long.parseLong(String.valueOf(info.get("offsaletime")))));
//            }
            if (null != info.get("onsaletime")) {
                try {
                    pet.setOnsaletime(DateFormat.getDateTimeInstance().parse(String.valueOf(info.get("onsaletime"))));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            return pet;
        }
        return null;
    }

    public static List<Shop> toListShop(JSONArray info) {
        ArrayList<Shop> list = new ArrayList<>();

        for (int i = 0; i < info.size(); i++) {
            Shop shop = new Shop();
            JSONObject jsonObject = info.getJSONObject(i);
            if (null != jsonObject.get("id")) {
                shop.setId(Long.valueOf(String.valueOf(jsonObject.get("id"))));
            }
            if (null != jsonObject.get("address")) {
                shop.setAddress(String.valueOf(jsonObject.get("address")));
            }
            if (null != jsonObject.get("name")) {
                shop.setName(String.valueOf(jsonObject.get("name")));
            }
            if (null != jsonObject.get("adminId")) {
                shop.setAdminId(Long.valueOf(String.valueOf(jsonObject.get("adminId"))));
            }
            list.add(shop);
        }
        return list;
//        UserAddress address = new UserAddress();
//        if ((info != null) && (!info.isEmpty())) {
//            if (null != info.get("id")) {
//                address.setId(Long.valueOf(String.valueOf(info.get("id"))));
//            }
//            if (null != info.get("contacts")) {
//                address.setContacts(String.valueOf(info.get("contacts")));
//            }
//            if (null != info.get("areaCode")) {
//                address.setAreaCode(String.valueOf(info.get("areaCode")));
//            }
//            if (null != info.get("address")) {
//                address.setAddress(String.valueOf(info.get("address")));
//            }
//            if (null != info.get("fullAddress")) {
//                address.setFullAddress(String.valueOf(info.get("fullAddress")));
//            }
//            if (null != info.get("phone")) {
//                address.setPhone(String.valueOf(String.valueOf(info.get("phone"))));
//            }
//            if (null != info.get("phoneBack")) {
//                address.setPhoneBack(String.valueOf(String.valueOf(info.get("phoneBack"))));
//            }
//            if (null != info.get("tel")) {
//                address.setTel(String.valueOf(String.valueOf(info.get("tel"))));
//            }
//            if (null != info.get("postCode")) {
//                address.setPostCode(String.valueOf(String.valueOf(info.get("postCode"))));
//            }
//            if (null != info.get("email")) {
//                address.setEmail(String.valueOf(info.get("email")));
//            }
//            if (null != info.get("user_id")) {
//                address.setUser_id(Long.valueOf(String.valueOf(info.get("user_id"))));
//            }
//            if (null != info.get("createTime")) {
//                try {
//                    address.setCreateTime(DateFormat.getDateTimeInstance().parse(String.valueOf(info.get("createTime"))));
//                } catch (ParseException e) {
//                    e.printStackTrace();
//                }
//            }
//            if (null != info.get("updateTime")) {
//                try {
//                    address.setUpdateTime(DateFormat.getDateTimeInstance().parse(String.valueOf(info.get("updateTime"))));
//                } catch (ParseException e) {
//                    e.printStackTrace();
//                }
//            }
//            return address;
//        }

    }

    public static Employee toEmp(JSONObject info) {
        Employee emp = new Employee();
        if ((info != null) && (!info.isEmpty())) {
            if (null != info.get("id")) {
                emp.setId(Long.valueOf(String.valueOf(info.get("id"))));
            }
            if (null != info.get("shopId")) {
                emp.setShopId(Long.valueOf(String.valueOf(info.get("shopId"))));
            }

            return emp;
        }
        return null;
    }

}
